using System;

using OpenXmlWriter.Packaging;

namespace OpenXmlWriter.WordprocessingML.Packaging
{
  public class WordprocessingDocumentWriter : OpenXmlPackage, IDisposable
  {
    //Fields

    #region Fields
    private WordprocessingDocumentType _docType;
    #endregion

    //Methods

    #region Methods
    protected WordprocessingDocumentWriter(string fileName, WordprocessingDocumentType docType)
      : this(fileName, docType, CompressionLevel.Normal)
    {
    }

    protected WordprocessingDocumentWriter(string fileName, WordprocessingDocumentType docType,
                                           CompressionLevel compressionLevel) : base(fileName, compressionLevel)
    {
      _docType = docType;
      PartsFactory.OpenXmlPackage = this;
    }

    public MainDocumentPart AddMainDocumentPart()
    {
      return (MainDocumentPart) AddNewPart(typeof (MainDocumentPart));
    }

    public CoreFilePropertiesPart AddCoreFilePropertiesPart()
    {
      return (CoreFilePropertiesPart) AddNewPart(typeof (CoreFilePropertiesPart));
    }

    public ExtendedFilePropertiesPart AddExtendedFilePropertiesPart()
    {
      return (ExtendedFilePropertiesPart) AddNewPart(typeof (ExtendedFilePropertiesPart));
    }

    public static WordprocessingDocumentWriter Create(string docTitle, WordprocessingDocumentType docType)
    {
      return new WordprocessingDocumentWriter(docTitle, docType);
    }

    public static WordprocessingDocumentWriter Create(string docTitle, WordprocessingDocumentType docType,
                                                      CompressionLevel compressionLevel)
    {
      return new WordprocessingDocumentWriter(docTitle, docType, compressionLevel);
    }

    protected override void Dispose(bool disposing)
    {
      if (!_disposed)
      {
        if (disposing)
        {
          Save();
          CleanUp();
        }
        _disposed = true;
      }
    }

    protected override string GenerateExtension()
    {
      switch (_docType)
      {
        case WordprocessingDocumentType.Template:
          return ".dotx";
        case WordprocessingDocumentType.Document:
        default:
          return ".docx";
      }
    }
    #endregion

    //Properties

    #region Properties
    public WordprocessingDocumentType DocumentType
    {
      get
      {
        return _docType;
      }
    }

    public override string TargetPath
    {
      get
      {
        return string.Empty;
      }
    }
    #endregion

    public override void AddTo(OpenXmlPartCollection parts)
    {
      ;
    }
  }
}